# coding=utf-8

import pygame
import sys
import pygame_gui
from timer import Timer
import group1
import group2
import group3
import group4
import group5

WIN_WIDTH = 1600
WIN_HEIGHT = 800

LABEL_HEIGHT = WIN_HEIGHT // 7
ANSWER_HEIGHT = LABEL_HEIGHT
QUESTION_HEIGHT = 2 * LABEL_HEIGHT

#ques_lib = group1.gen_ques(50, swap=True)
#ques_lib = group2.gen_ques(50, category=20)
#ques_lib = group3.gen_ques(50, swap=True)
#ques_lib = group4.gen_ques(50, swap=True)
ques_lib = group5.gen_ques(50, swap=True)

ques_idx = 0
ques_new = True
answ_idx = -1
answ_sel_idx = -1

ques_count = 0
correct_count = 0
wrong_count = 0


timer = Timer()
timer.set_change_func(lambda x: label_time.set_text("时间：%s" % Timer.str_time(x)))


def select_correct():
    global correct_count
    correct_count += 1
    label_correct.set_text("正确数：%d" % correct_count)
    print("correct!")


def select_wrong():
    global wrong_count
    wrong_count += 1
    label_wrong.set_text("错误数：%d" % wrong_count)
    print("wrong!")


# 初始化 pygame
pygame.init()

# 创建一个窗口
root_sf = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))

# 创建 GUI 管理器
manager = pygame_gui.UIManager((WIN_WIDTH, WIN_HEIGHT), 'theme.json')

label_no = pygame_gui.elements.UILabel(
    relative_rect=pygame.Rect((0, 0), (300, LABEL_HEIGHT)),
    text="第 1 题",
    manager=manager,
    object_id=pygame_gui.core.ObjectID(object_id='#label_no')
)

label_correct = pygame_gui.elements.UILabel(
    relative_rect=pygame.Rect((0, 0), (300, LABEL_HEIGHT)),
    text="正确数：0",
    manager=manager,
    object_id=pygame_gui.core.ObjectID(object_id='#label_correct'),
     anchors = {"top": "top",
                "right": "left",
                "left_target": label_no}
)

label_wrong = pygame_gui.elements.UILabel(
    relative_rect=pygame.Rect((0, 0), (300, LABEL_HEIGHT)),
    text="错误数：0",
    manager=manager,
    object_id=pygame_gui.core.ObjectID(object_id='#label_wrong'),
     anchors = {"top": "top",
                "right": "left",
                "left_target": label_correct}
)

label_time = pygame_gui.elements.UILabel(
    relative_rect=pygame.Rect((0, 0), (300, LABEL_HEIGHT)),
    text="时间：00:00",
    manager=manager,
    object_id=pygame_gui.core.ObjectID(object_id='#label_time'),
     anchors = {"top": "top",
                "right": "left",
                "left_target": label_wrong}
)

textbox_question = pygame_gui.elements.UITextBox(
    relative_rect=pygame.Rect((0, LABEL_HEIGHT), (WIN_WIDTH, QUESTION_HEIGHT)),
    html_text="时间：00:00",
    manager=manager,
    object_id=pygame_gui.core.ObjectID(object_id='#text_box_question'),
    #  anchors = {"top": "top",
    #             "right": "left",
    #             "left_target": label_wrong}
)

button_answers = []
for i in range(4):
    button_answer = pygame_gui.elements.UIButton(
        relative_rect=pygame.Rect((0, i*ANSWER_HEIGHT + QUESTION_HEIGHT + LABEL_HEIGHT), (WIN_WIDTH, ANSWER_HEIGHT)),
        text="",
        manager=manager,
        object_id=pygame_gui.core.ObjectID(class_id='@button_answer_left' if i % 2 == 0 else '@button_answer_right')
                                           #object_id='#hello_button')#,
        # object_id=pygame_gui.core.ObjectID(object_id='#label_time'),
        #  anchors = {"top": "top",
        #             "right": "left",
        #             "left_target": label_wrong}
    )
    button_answers.append(button_answer)

timer.start_timer()

# 主循环
clock = pygame.time.Clock()
while True:
    time_delta = clock.tick(60) / 1000.0  # 帧率控制

    if ques_new:
        print("ques_idx = %d, len(ques_lib) = %d" % (ques_idx, len(ques_lib)))
        ques = ques_lib[ques_idx]

        textbox_question.set_text(ques[0])
        for ans_idx in range(0, 4):
            button_answers[ans_idx].set_text(ques[ans_idx + 1])
            button_answers[ans_idx].enable()

        answ_idx = ques[5]
        ques_idx += 1

        label_no.set_text("第 %d 题" % ques_idx)

        ques_new = False

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                print("A key pressed")
            if event.key == pygame.K_b:
                print("B key pressed")

        elif event.type == pygame_gui.UI_BUTTON_PRESSED:
            answ_sel_idx = -1
            for i, button_answer in enumerate(button_answers):
                if event.ui_element == button_answer:
                    answ_sel_idx = i
                    if answ_sel_idx == answ_idx:
                        select_correct()
                        ques_new = True
                    else:
                        button_answer.disable()
                        select_wrong()
                    break

        manager.process_events(event)

    manager.update(time_delta)

    timer.tick_timer()
    root_sf.fill((255, 255, 255))

    # 刷新窗口
    manager.draw_ui(root_sf)
    pygame.display.update()
